package com.bdqn.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.demo.mapper.SysUserMapper;
import com.bdqn.demo.pojo.SysUser;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

/**
 * SysUserMapperTester
 *
 * @author f
 * @since 2021/11/27
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class SysUserMapperTester {
    @Resource
    private SysUserMapper sysUserMapper;

    @Test
    public void testSelectById(){// 主键查询
        SysUser sysUser = sysUserMapper.selectById(2L);
        System.out.println("usrName："+sysUser.getUsrName());
    }

    @Test
    public void testInsert() {// 添加
        sysUserMapper.insert(new SysUser("MyBatis-Plus","123456",8L,1L));
    }

       @Test
    public void testFindAll() {// 查询所有
       List<SysUser> list = sysUserMapper.selectList(null);
       for(SysUser sysUser:list){
         System.out.println("usrName："+sysUser.getUsrName());
         }
       }

    @Test
 public void testFind() {// 条件查询：根据用户名和密码查询
      QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
      wrapper.eq("usr_name","MyBatis-Plus");
      wrapper.eq("usr_password","123456");
      List<SysUser> list = sysUserMapper.selectList(wrapper);
      if(list!=null){
         for(SysUser sysUser:list){
         System.out.println("usrId："+sysUser.getUsrId());
         }
      }
   }


    @Test
    public void testPagination() {// 分页查询
        QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
        wrapper.eq("usr_role_id",2L);//查询条件：角色 Id 为 2
        //分页对象：查询获得 User 对象，查看第 1 页数据，每页显示 2 条记录
        Page<SysUser> page = new Page<SysUser>(1, 2);
        IPage<SysUser> sysUserPager = sysUserMapper.selectPage(page,wrapper);
        System.out.println("总记录数 ---- --> " + sysUserPager.getTotal());
        System.out.println("总页数 ------ --> " + sysUserPager.getPages());
        System.out.println("当前页数 ----- -> " + sysUserPager.getCurrent());
        System.out.println("每页记录数 -----> " + sysUserPager.getSize());
        System.out.println("当前记录： -----> ");
        for(SysUser sysUser:sysUserPager.getRecords()){//获得查询记录
            System.out.println("usrId："+sysUser.getUsrId());
        }
    }
}
